<?php
class TaskNotification extends AppModel{
	public $name='TaskNotification';
	
	public $belongsTo = array(
		 'Task' => array(
		 'className' => 'Task',
		 'foreignKey' => 'task_id'
		 )
	);
	
	public function notify($userid=0,$taskid=0){
		//notify all user relevant except this userid		
		$taskid=intval($taskid);
		if($taskid){
			$data=$this->updateAll(array($this->name.'.published'=>1),array('NOT'=>array($this->name.'.user_id'=>$userid),$this->name.'.task_id'=>$taskid));
		}
	}
	
public function notifyToUserId($userid=0,$taskid=0){
		//notify only userid	
		$taskid=intval($taskid);
		if($taskid){
			$data=$this->updateAll(array($this->name.'.published'=>1),array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid));
		}
	}
	
	public function unnotify($userid=null,$taskid=0){
		$taskid=intval($taskid);
		if($userid && $taskid){
			$data=$this->updateAll(array($this->name.'.published'=>0),array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid));
		}
	}
	
	public function addWatch($userid=0,$taskid=0){
		$userid=intval($userid);
		$taskid=intval($taskid);
		if($userid && $taskid){
			//check exist
			$data=$this->find('first',array('conditions'=>array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid)));
			if(empty($data)){
				//insert
				$data=array('published'=>0,'user_id'=>$userid,'task_id'=>$taskid,'watch'=>1);
				$this->create($data);
				$this->save();
			}
			elseif (!$data['TaskNotification']['watch']){
				//update
				$this->updateAll(array('watch'=>1),array($this->name.'.id'=>$data['TaskNotification']['id']));
			}		
		}
	}
	
	public function removeWatch($userid=0,$taskid=0){
		$userid=intval($userid);
		$taskid=intval($taskid);
		if($userid && $taskid){
			$data=$this->deleteAll(array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid));
		}
	}
	
	
	public function addNotify($userid=0,$taskid=0){
		$userid=intval($userid);
		$taskid=intval($taskid);
		if($userid && $taskid){
			//check exist
			$data=$this->find('first',array('conditions'=>array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid)));
			if(empty($data)){
				//insert
				$data=array('published'=>0,'user_id'=>$userid,'task_id'=>$taskid);
				$this->create($data);
				$this->save();
			}
			
		}
	}
	
	public function removeNotify($userid=0,$taskid=0){
		$userid=intval($userid);
		$taskid=intval($taskid);
		if($userid && $taskid){
			$data=$this->deleteAll(array($this->name.'.user_id'=>$userid,$this->name.'.task_id'=>$taskid,$this->name.'.watch <>'=>1));
		}
	}
	
	public function getAllByUser($userid=0){
		$data=array();
		$userid=intval($userid);
		if($userid){
			$data=$this->find('list',array('fields'=>array('task_id','task_id'),'conditions'=>array($this->name.'.user_id'=>$userid,$this->name.'.published'=>1)));
		}
		return $data;
	}
	
	public function getAllNonByUser($userid=0){
		$data=array();
		$userid=intval($userid);
		if($userid){
			$data=$this->find('list',array('fields'=>array('task_id','task_id'),'conditions'=>array($this->name.'.user_id'=>$userid,$this->name.'.published'=>0)));
		}
		return $data;
	}
}
?>
